﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Group_Project_Book_Store
{
    class InvoiceItems
    {
        /// <summary>
        /// get and set for list of items
        /// </summary>
        public List<item> listOfItems { get; set; }
        public InvoiceItems()
        {
            listOfItems = new List<item>();
        }
        /// <summary>
        /// add book item
        /// </summary>
        /// <param name="book"></param>
        /// <returns></returns>
        public List<item> AddItem(clsBook book)
        {
           try 
	        {	        
		         var templist = new List<item>();
                item item = new item { bookTitle = book.bookTitle, Quantity = 1, bookCost = book.bookCost, lineTotal = book.bookCost, book = book };
                listOfItems.Add(item);

                var groupedListOfItems = (from items in listOfItems group items by items.bookTitle into newGroup select newGroup);

                foreach (var itemGroup in groupedListOfItems)
                {

                    item invoice = new item();

                    invoice.bookTitle = itemGroup.First().bookTitle;
                    invoice.bookCost = itemGroup.First().bookCost;
                    invoice.Quantity = itemGroup.ToList().Count();
                    invoice.lineTotal = invoice.bookCost * invoice.Quantity;
                    invoice.book = itemGroup.First().book;

                    templist.Add(invoice);
                }
                return templist;
                }
               catch (Exception)
               {

                   throw;
               }
	        }
	       
        }
        class item
        {
            /// <summary>
            /// get and set book title
            /// </summary>
            public string bookTitle { get; set; }
            /// <summary>
            /// get and set quantity of book
            /// </summary>
            public int Quantity { get; set; }
            /// <summary>
            /// get and set book cost
            /// </summary>
            public double bookCost { get; set; }
            /// <summary>
            /// get and set line total
            /// </summary>
            public double lineTotal { get; set; }
            /// <summary>
            /// get and set book
            /// </summary>
            public clsBook book { get; set; }
        } 
}
